#include "util.hpp"
#include "Message.hpp"
#include "format.hpp"
#include "logLevel.hpp"
#include "sink.hpp"
#include "logger.hpp"
#include "sqylog.hpp"
#include "buffer.hpp"
using std::endl;
using std::cout;

void test_log()
{
    sqy_log::Logger::ptr logger = sqy_log::LoggerMangger::getInstance().getLogger("async_logger");
    logger->debug("%s","测试日志");
    logger->info("%s","测试日志");
    logger->warn("%s","测试日志");
    logger->error("%s","测试日志");
    logger->fatal("%s","测试日志");
    size_t count1 = 500000;
    size_t cursize = 0,count = 0;
    while(cursize < count1)
    {
        logger->fatal("测试日志-%d",count++);
        cursize ++;
    }
    // DEBUG("%s","测试日志");
    // INFO("%s","测试日志");
    // WARN("%s","测试日志");
    // ERROR("%s","测试日志");
    // FATAL("%s","测试日志");

}
int main()
{
    //测试单例
    std::unique_ptr<sqy_log::LoggerBuilder> builder(new sqy_log::GlobalLoggerBuilder());
    builder->buildFormatter("[%c][%f:%l]%m%n");
    builder->buildLoggerLevel(sqy_log::LogLevel::value::WARING);
    builder->buildLoggerName("async_logger");
    // builder->buildEnableUnsafeAsync();
    builder->buildLoggerType(sqy_log::LoggerType::LOGGER_ASYNC);
    builder->buildSink<sqy_log::FileSink>("./logfile/async.log");
    builder->buildSink<sqy_log::StdoutSink>();
    builder->build();
    test_log();

    //测试Async
    // std::unique_ptr<sqy_log::LoggerBuilder> builder(new sqy_log::LocalLoggerBuilder());
    // builder->buildFormatter("[%c]%m%n");
    // builder->buildLoggerLevel(sqy_log::LogLevel::value::WARING);
    // builder->buildLoggerName("async_logger");
    // builder->buildEnableUnsafeAsync();
    // builder->buildLoggerType(sqy_log::LoggerType::LOGGER_ASYNC);
    // builder->buildSink<sqy_log::FileSink>("./logfile/async.log");
    // builder->buildSink<sqy_log::StdoutSink>();
    // sqy_log::Logger::ptr logger = builder->build();

    // logger->debug(__FILE__,__LINE__,"%s","测试日志");
    // logger->info(__FILE__,__LINE__,"%s","测试日志");
    // logger->warn(__FILE__,__LINE__,"%s","测试日志");
    // logger->error(__FILE__,__LINE__,"%s","测试日志");
    // logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    // size_t count1 = 500000;
    // size_t cursize = 0,count = 0;
    // while(cursize < count1)
    // {
    //     logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
    //     cursize ++;
    // }

    //测试buffer
    //读取文件数据,一点一点写入缓冲区，最终将缓冲区数据写入文件，判断生成的新文件与源文件是否一致
    // std::ifstream ifs("./logfile/test.log",std::ios::binary);
    // if(ifs.is_open() == false) {return -1;}
    // ifs.seekg(0,std::ios::end);//读写位置跳转到文件末尾
    // size_t fsize = ifs.tellg();//获取当前读写位置相对于起始位置的偏移量
    // ifs.seekg(0,std::ios::beg);//读写位置跳转到文件起始位置
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0],fsize);//这里不能使用c_str的原因是因为c_str返回的是const char*，不能修改，所以不能使用
    // if(ifs.good() == false) {std::cout << "read error" << std::endl;}
    // ifs.close();
    // sqy_log::Buffer buffer;
    // for(int i = 0; i < fsize; i++)
    // {
    //     buffer.push(&body[i],1);
    // }

    // std::ofstream ofs("./logfile/tmp.log",std::ios::binary);
    // size_t n = buffer.readAbleSize();//提前保存，因为将读写位置向后偏移双方相对移动，导致只能读取一半的数据
    // for(int i = 0; i < n ; i++)
    // {
    //     ofs.write(buffer.begin(),1);
    //     buffer.moveReader(1);
    // }
    // ofs.close();

    //测试建造者
    // std::unique_ptr<sqy_log::LoggerBuilder> builder(new sqy_log::LocalLoggerBuilder());
    // builder->buildFormatter("%m%n");
    // builder->buildLoggerLevel(sqy_log::LogLevel::value::WARING);
    // builder->buildLoggerName("sync_logger");
    // builder->buildLoggerType(sqy_log::LoggerType::LOGGER_SYNC);
    // builder->buildSink<sqy_log::FileSink>("./logfile/test.log");
    // builder->buildSink<sqy_log::StdoutSink>();
    // sqy_log::Logger::ptr logger = builder->build();

    //测试Logger
    // std::string logger_name = "sync_logger";
    // sqy_log::LogLevel::value limit = sqy_log::LogLevel::value::WARING;
    // sqy_log::Formatter::ptr fmt(new sqy_log::Formatter("[%d{%H:%M:%S}][%c][%f:%l][%p]%T%m%n"));
    // sqy_log::LogSink::ptr stdout_lsp = sqy_log::SinkFactory::create<sqy_log::StdoutSink>();
    // sqy_log::LogSink::ptr file_lsp = sqy_log::SinkFactory::create<sqy_log::FileSink>("./logfile/test.log");
    // sqy_log::LogSink::ptr roll_lsp = sqy_log::SinkFactory::create<sqy_log::RollBySizeSink>("./logfile/roll-",1024*1024);
    // std::vector<sqy_log::LogSink::ptr> sinks = {stdout_lsp,file_lsp,roll_lsp};
    // sqy_log::Logger::ptr logger(new sqy_log::SyncLogger(logger_name,limit,fmt,sinks));

    // logger->debug(__FILE__,__LINE__,"%s","测试日志");
    // logger->info(__FILE__,__LINE__,"%s","测试日志");
    // logger->warn(__FILE__,__LINE__,"%s","测试日志");
    // logger->error(__FILE__,__LINE__,"%s","测试日志");
    // logger->fatal(__FILE__,__LINE__,"%s","测试日志");
    // size_t count1 = 500000;
    // size_t cursize = 0,count = 0;
    // while(cursize < count1)
    // {
    //     logger->fatal(__FILE__,__LINE__,"测试日志-%d",count++);
    //     cursize ++;
    // }

    //测试Sink
    // sqy_log::LogMsg msg(sqy_log::LogLevel::value::INFO,53,"main.c","root","格式化功能测试...");
    // sqy_log::Formatter fmt("abc%%cde[%d{%H:%M:%S}][%p]%T%m%n");
    // std::string str = fmt.format(msg);

    // sqy_log::LogSink::ptr stdout_lsp = sqy_log::SinkFactory::create<sqy_log::StdoutSink>();
    // sqy_log::LogSink::ptr file_lsp = sqy_log::SinkFactory::create<sqy_log::FileSink>("./logfile/test.log");
    // sqy_log::LogSink::ptr roll_lsp = sqy_log::SinkFactory::create<sqy_log::RollBySizeSink>("./logfile/roll-",1024*1024);
    // sqy_log::LogSink::ptr time_lsp = sqy_log::SinkFactory::create<sqy_log::RollByTimeSink>("./logfile/roll-",sqy_log::TimeGap::GAP_SECOND);
    // time_t old = sqy_log::util::Date::now();
    // while(sqy_log::util::Date::now() < old + 5)
    // {
    //     time_lsp->log(str.c_str(),str.size());
    // }
    // stdout_lsp->log(str.c_str(),str.size());
    // file_lsp->log(str.c_str(),str.size());
    // size_t cursize = 0;
    // int count = 0;
    // while(cursize < 1024*1024*10)
    // {
    //     std::string tmp = str + std::to_string(count++);
    //     roll_lsp->log(tmp.c_str(),tmp.size());
    //     cursize += tmp.size();
    // }

    // std::cout << str <<std::endl;
    // std::cout << sqy_log::util::Date::now() << std::endl;
    // std::string pathname = "./abc/bcd/ert" ;
    // std::cout << sqy_log::util::File::path(pathname) << std::endl;
    // sqy_log::util::File::createDirectory(pathname);
    // cout << sqy_log::LogLevel::toString(sqy_log::LogLevel::value::DEBUG) <<endl;
    // cout << sqy_log::LogLevel::toString(sqy_log::LogLevel::value::INFO) <<endl;
    // cout << sqy_log::LogLevel::toString(sqy_log::LogLevel::value::WARING) <<endl;
    // cout << sqy_log::LogLevel::toString(sqy_log::LogLevel::value::ERROR) <<endl;
    // cout << sqy_log::LogLevel::toString(sqy_log::LogLevel::value::FATAL) <<endl;
    // cout << sqy_log::LogLevel::toString(sqy_log::LogLevel::value::OFF) <<endl;
    return 0;
}